<?php

namespace App\Services\Scenic;


class ScenicServiceTools
{

    //A.自然风光（以天池,草原,地质公园,丽景,生态公园结尾）
    //B.温泉（含温泉字）
    //C.其他景点（以基地,乐园,巴扎,博览园,民俗园,发电站结尾）,
    //D.动物园（含动物园）
    //E.博物馆（含博物馆）
    //F.其他展馆展览（以体验馆结尾）
    //G.公园游乐场（以公园,水上乐园,水世界,大世界结尾）
    //H.滑雪（含滑雪）
    //I.植物园（以植物园结尾）
    //J.名胜古迹（含佛洞,故城,塔,坎儿井）
    public static function getScenicClass($scenicName)
    {
        $length = mb_strlen($scenicName);
        $rules = [
            'A' => ['rule' => 'last', 'keywords' => '天池,草原,地质公园,丽景,生态公园'],
            'B' => ['rule' => 'has', 'keywords' => '温泉'],
            'C' => ['rule' => 'last', 'keywords' => '基地,乐园,巴扎,博览园,民俗园,发电站'],
            'D' => ['rule' => 'has', 'keywords' => '动物园'],
            'E' => ['rule' => 'has', 'keywords' => '博物馆'],
            'F' => ['rule' => 'last', 'keywords' => '体验馆'],
            'G' => ['rule' => 'last', 'keywords' => '公园,水上乐园,水世界,大世界'],
            'H' => ['rule' => 'has', 'keywords' => '滑雪'],
            'I' => ['rule' => 'last', 'keywords' => '植物园'],
            'J' => ['rule' => 'has', 'keywords' => '佛洞,故城,塔,坎儿井'],
        ];
        foreach ($rules as $class => $rule) {
            $ruleKeywords = explode(',', $rule['keywords']);
            foreach ($ruleKeywords as $keyword) {
                if ($rule['rule'] == 'has') {
                    if (strpos($scenicName, $keyword) !== false) {
                        return $class;
                    }
                } elseif ($rule['rule'] == 'last') {
                    $keywordLength = mb_strlen($keyword);
                    if ($keywordLength < $length) {
                        $tmp = substr($scenicName, -$keywordLength);
                        if ($tmp == $keyword) {
                            return $class;
                        }
                    }
                }
            }
        }
        return '';
    }

    private static $certificateTypeValues = [1, 2, 3, 4, 7,];
    /**
     * 证件类型转化为加密数字
     * @param $certificateType
     * @return int
     */
    public static function enIdentityType(string $certificateType) : int
    {
        $certificateType = empty($certificateType) ? 1: $certificateType;
        $identityType = 0;
        $len = count(self::$certificateTypeValues);
        if ($certificateType) {
            $identityTypes = explode(',', $certificateType);
            foreach ($identityTypes as $identity) {
                foreach (self::$certificateTypeValues as $index=>$type){
                    if ($type == $identity){
                        $value = pow(2, $len - 1 - $index);
                        $identityType += $value;
                        break;
                    }
                }
            }
        }
        return $identityType;
    }

    /**
     * 证件类型加密数字转化为具体类型
     * @param int $value
     * @return string
     */
    public static function deIdentityType(int $value) :string
    {

        //数字转化成2进制值
        $bin       = decbin($value); // 转2进制
        $len       = strlen($bin);
        $arr       = [];
        for ($i = 0; $i < $len; $i++) {
            if ($bin[$i] == 1) {
                $arr[] = self::$certificateTypeValues[$i] ?? '';
            }
        }
        return implode(',',$arr);
    }


    /**
     * 经纬度顺序转换
     * @param $location
     * @return string
     */
    public static function changeLocation($location)
    {
        if (empty($location)){
            return $location;
        }
        $locationArr = explode(',', $location);
        $lat = $locationArr[1] ?? '';
        $locationArr[1] = $locationArr[0];
        $locationArr[0] = $lat;
        foreach ($locationArr as $key=>$value){
            if ($value === null || $value == 'null'){
                unset($locationArr[$key]);
            }
        }
        return implode(',', $locationArr);
    }

    /**
     * 获取内容图片
     * @param string $content
     * @return array
     */
    public static function getHtmlImages(string $content):array
    {
        preg_match_all('/<img.+?src="(.+?)"/', $content, $matches);

        // $matches[1]中包含了所有图片链接
        $image_links = $matches[1] ?? [];

        $images = [];
        // 获取所有图片链接
        foreach ($image_links as $link) {
            $images[] = ['type'=>2,'url'=>$link];
        }

        return $images;
    }


    /**
     * 点位转换：百度转高德
     * @param {*} bd_lng longitude（经度）
     * @param {*} bd_lat latitude（纬度）
     * @returns [longitude, latitude]
     */
    public static function bMapToGeo(string $location)
    {
        if (empty($location)){
            return $location;
        }
        [$bd_lng, $bd_lat] = explode(',', urldecode($location));
        $X_PI = (pi() * 3000.0) / 180;
        $x = (float)$bd_lng - 0.0065;
        $y = (float)$bd_lat - 0.006;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $X_PI);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $X_PI);
        $gg_lng = $z * cos($theta);
        $gg_lat = $z * sin($theta);
        return $gg_lng . ',' . $gg_lat;
    }

    /**
     * 点位转换：高德转百度
     * @param {*} gd_lng longitude（经度）
     * @param {*} gd_lat latitude（纬度）
     * @returns [longitude, latitude]
     */
    public static function geoToBMap(string $location)
    {
        if (empty($location)){
            return $location;
        }
        [$gd_lng, $gd_lat] = explode(',', urldecode($location));
        $X_PI = (pi() * 3000.0) / 180;
        $x = (float)$gd_lng;
        $y = (float)$gd_lat;
        $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $X_PI);
        $theta = atan2($y, $x) + 0.000003 * cos($x * $X_PI);
        $bd_lng = $z * cos($theta) + 0.0065;
        $bd_lat = $z * sin($theta) + 0.006;
        return $bd_lng . ',' . $bd_lat;
    }

    /**
     * 经纬度长度处理
     * @param string $location
     * @param int $n
     * @return string
     */
    public static function dealLocation(string $location,int $n = 6) :string
    {
        if (empty($location)){
            return $location;
        }
        [$lng, $lat] = explode(',', urldecode($location));
        $num = pow(10,$n);
        $lng = bcdiv(bcmul($lng,$num,0),$num,$n);
        $lat = bcdiv(bcmul($lat,$num,0),$num,$n);
        return $lng . ',' . $lat;
    }

}
